package com.study.springboottest.sort;

public class BubbleSortTest {

    public static void main(String[] args) {
        int[] arr = {5, 3, 15, 50, 62, 21, 28, 34, 64, 91, 67};
        int[] ints = bubbleSort(arr);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(arr[i]);
        }

        System.out.println("============================================");

        int[] newInts = sort(arr);
        for (int i = 0; i < newInts.length; i++) {
            System.out.println(newInts[i]);
        }

        int[] newInts1 = sort(arr);
        for (int i = 0; i < newInts1.length; i++) {
            System.out.println(newInts[i]);
        }
    }

    /**
     * 冒泡排序
     *
     * @param arr
     * @return
     */
    public static int[] bubbleSort(int[] arr) {
        int size = arr.length;
        for (int i = 0; i < size - 1; i++) {
            for (int j = 0; j < size - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

    //默写
    public static int[] sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

}
